<template>
<el-drawer class="drawer" :title="drawer.title" :visible.sync="drawer.visible" direction="rtl" custom-class="drawer-right-side" :before-close="closeDrawer" :append-to-body="true">
    <el-form class="ruleForm" ref="ruleForm" :model="ruleForm" :rules="rules" label-position="top" label-width="100px">
        <el-form-item label="父级渠道" prop="parent_id">
            <el-cascader v-model="ruleForm.parent_id" :options="categoryTree" :props="{
            checkStrictly: true,
            label: 'title',
            value: 'id',
            emitPath: false,
          }" clearable></el-cascader>
        </el-form-item>

        <el-form-item label="分类名称" prop="title">
            <el-input v-model="ruleForm.title" placeholder="分类名称"></el-input>
        </el-form-item>

        <el-form-item label="分类图片" prop="big_pic">
            <upload-picture ref="big_picRef" v-model="ruleForm.big_pic" placeholder="1张;尺寸:1000*1000;格式:jpg,png,jpeg;大小:30M以内"></upload-picture>
        </el-form-item>

        <el-form-item label="是否独立售卖">
            <el-radio-group v-model="ruleForm.is_sale">
                <el-radio :label="0">否</el-radio>
                <el-radio :label="1">是</el-radio>
            </el-radio-group>
        </el-form-item>

        <el-form-item label="关联企业信息">
            <el-radio-group v-model="ruleForm.on_member_company">
                <el-radio :label="0">否</el-radio>
                <el-radio :label="1">是</el-radio>
            </el-radio-group>
        </el-form-item>

        <el-form-item label="排序" prop="sort">
            <el-input v-model="ruleForm.sort" type="number" placeholder="排序"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="submit">确定并提交</el-button>
        </el-form-item>
    </el-form>
</el-drawer>
</template>

<script>
import {
    getFormToken
} from "@/api/system/system";
import uploadPicture from '@/components/upload/picture.vue';

import {
    getCategoryTreeApi,
    readCategoryApi,
    createCategoryApi,
    editCategoryApi
} from '@/api/product/category'
const ruleForm = {
    id: "",
    parent_id: "",
    title: "",
    big_pic: '',
    sort: 0,
    is_member_category: 0,
    customer_model: "",
    order_model: "",
    is_sale: 0,
    on_member_company: 0,
    is_four: 0,
    form_token: ""
};

export default {
    components: {
        uploadPicture
    },
    data() {
        return {
            drawer: {
                title: "添加分类",
                visible: false,
                type: "create"
            },
            categoryTree: [],
            ruleForm: Object.assign({}, ruleForm),
            rules: {
                title: [{
                    required: true,
                    message: "请填写分类名称",
                    trigger: "change",
                }, ],
                big_pic: [{
                    required: true,
                    message: "请上传分类图片",
                }, ],
            },
        };
    },
    methods: {
        showCreate() {
            this.ruleForm = Object.assign({}, ruleForm);
            this.drawer.visible = true;
            this.drawer.type = "create";
            this.drawer.title = "添加分类";
            this.getCategoryTree();

            // 获取表单的token
            getFormToken({
                api: "/api/product.category/create",
            }).then(res => {
                this.ruleForm.form_token = res.data.token;
            });
        },
        showEdit(row) {
            this.ruleForm = Object.assign({}, row);
            this.drawer.title = "编辑分类";
            this.drawer.visible = true;
            this.drawer.type = "edit";
            this.getCategoryTree();

            // 获取表单的token
            getFormToken({
                api: "/api/product.category/edit",
            }).then(res => {
                this.ruleForm.form_token = res.data.token;
            });
        },
        closeDrawer() {
            this.drawer.visible = false;
        },
        getCategoryTree() {
            getCategoryTreeApi().then(res => {
                if (res.code == "200") {
                    this.categoryTree = res.data.tree;
                } else {
                    this.$message.error(res.msg);
                }
            })
        },
        submit() {
            this.$refs["ruleForm"].validate((valid) => {
                if (!valid) return false;

                if (this.drawer.type == "create") {
                    createCategoryApi(this.ruleForm).then(res => {
                        if (res.code == "200") {
                            this.$emit("getTableData");
                            this.closeDrawer();
                            this.$message.success(res.msg);
                        } else {
                            this.$message.error(res.msg);
                        }
                    })
                } else {
                    editCategoryApi(this.ruleForm).then(res => {
                        if (res.code == "200") {
                            this.$emit("getTableData");
                            this.closeDrawer();
                            this.$message.success(res.msg);
                        } else {
                            this.$message.error(res.msg);
                        }
                    })
                }
            });
        },
    },
};
</script>

<style>
.contacts {
    margin: 18px 0;
}

.w100 {
    width: 100%;
}
</style>
